package com.backend.service.rbac.impl;

import cn.hutool.core.bean.BeanUtil;
import com.backend.mapper.rbac.RoleAuthMapper;
import com.backend.service.rbac.RoleAuthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pojo.entity.Auth;
import com.pojo.entity.RoleAuth;
import com.pojo.vo.AuthVO;
import com.pojo.vo.RoleAuthVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class RoleAuthServiceImpl extends ServiceImpl<RoleAuthMapper, RoleAuth> implements RoleAuthService {
    @Autowired
    RoleAuthMapper roleAuthMapper;
    @Override
    public List<AuthVO> getAuthsByRoleId(Long id) {
        List<Auth> auths = roleAuthMapper.getAuthsByRoleId(id);
        List<AuthVO> authVOS = auths.stream().map(auth -> {
            return BeanUtil.copyProperties(auth, AuthVO.class);
        }).collect(Collectors.toList());
        return authVOS;
    }

    @Override
    public Integer removeAuthsByRoleId(Long id) {
        Integer result = roleAuthMapper.removeAuthsByRoleId(id);
        return result;
    }

    @Override
    public Integer updateAuthByRoleId(Long id, List<Long> authIds) {
        Integer result = roleAuthMapper.insertAuthByRoleId(id, authIds);
        return result;
    }
}
